package com.invengo.module_check.ui.fragment.parallel.item;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.module_check.R;
import com.invengo.module_check.ui.fragment.parallel.ParallelViewModel;

import androidx.annotation.NonNull;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT_IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_NO_LISTING;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_OTHER_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT;
import static com.invengo.library_db.entity.module_check.BookState.NEED_IN_SHELF;

import android.text.TextUtils;

public class ParallelItemViewModel extends ItemViewModel<ParallelViewModel> {

    public SingleLiveEvent<BookBean> book = new SingleLiveEvent<>();
//    public SingleLiveEvent<String> bookName = new SingleLiveEvent<>();
//    public SingleLiveEvent<String> barCode = new SingleLiveEvent<>();
//    public SingleLiveEvent<String> callNo = new SingleLiveEvent<>();
//    public SingleLiveEvent<String> shelfName = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> tipColor = new SingleLiveEvent<>();
    public SingleLiveEvent<String> tipContent = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isForce = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isFirst = new SingleLiveEvent<>();

    public ParallelItemViewModel(@NonNull ParallelViewModel viewModel, BookBean bean) {
        super(viewModel);
//        bookName.setValue(bean.getBookName());
//        barCode.setValue(bean.getBarCode());
//        callNo.setValue(bean.getCallNo());
//        shelfName.setValue(bean.getShelfName());
        updateState(bean);
    }

    public void updateState(BookBean bean) {
        book.setValue(bean);
        String firstBookBarCode = viewModel.getFirstBookBarCode();
        if (ACache.getInstance().getAsBoolean(AKey.FEATURE_MODULE)) {
            boolean first = !TextUtils.isEmpty(firstBookBarCode) && bean.getBarCode().equals(firstBookBarCode);
            isFirst.setValue(first);
            isForce.setValue(first ? false : bean.getForceSortCase());
        } else {
            isFirst.setValue(false);
            isForce.setValue(false);
        }
        switch (bean.getTip()) {
            case ERROR_OTHER_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
//                tipContent.setValue(bean.getTip() );
                tipContent.setValue(bean.getTip() + "\n" + bean.getShelfName());
                viewModel.addError(bean);
                break;
            case ERROR_NO_LISTING:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.setValue(bean.getTip());
                viewModel.addError(bean);
                break;
            case LENG_OUT_IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.setValue(bean.getTip() + "\n 请取出");
                break;
            case LENG_OUT:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_orange));
                tipContent.setValue(bean.getTip() + (StringUtils.isEmpty(bean.getNeedBackDate()) ? " 请取出" : ("\n 应归日期：" + bean.getNeedBackDate())));
                viewModel.addLengOut(bean);
                break;
            case IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_green));
                tipContent.setValue(bean.getTip());
                viewModel.addInShelf(bean);
                break;
            case NEED_IN_SHELF:
                tipColor.setValue(viewModel.getApplication().getColor(R.color.color_blue));
                tipContent.setValue(bean.getTip());
                break;
            default:
                KLog.e(">>>  " + bean.toString());
                break;
        }
    }

    public int getPosition() {
        return viewModel.getPosition(this) + 1;
    }

    public BindingCommand removeItem = new BindingCommand(() -> {
        viewModel.onItemRemove(this);
    });
}
